package com.taxsystem.chenliangli.askquestion_tool;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


/*
 * 
 * 这个类是用SQLite数据库的
 * 
 * */
public class SiteDBHlp extends SQLiteOpenHelper {
	
    public static long x=0;
	private static String database_name="site";
	private static int version=1;
	private static final String table_name="siteinfo";
	private static final String table_create=
			"create table "+table_name+" ( "
			+" id varchar(10) not null, "+
			" name varchar(30) not null, "+
			" phonoNo varchar(20), "+
			" address varchar(100),primary key(id)); ";
	 
			

	public SiteDBHlp(Context context) 
	{
		super(context, database_name, null, version);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		db.execSQL(table_create);
       // fillDB();
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub

	}
	//初始化数据库中的数据
   public	void fillDB()
   {
	   SQLiteDatabase db=getWritableDatabase();
	   ContentValues[]values=new ContentValues[3];
	   for(int i=0;i<values.length;i++)
		   values[i]=new ContentValues();
	   values[0].put("id", "yuanmingshan");
	   values[0].put("name", "阳明山公园");
	   values[0].put("phonoNo", "020-28613601");
	   values[0].put("address", "台北市北投区竹子湖路1之20号");
	   
	   values[1].put("id", "yushan");
	   values[1].put("name", "玉山国家公园管理处");
	   values[1].put("phonoNo", "049-2773121");
	   values[1].put("address", "南投县水里乡中山路一段300号");
	   
	   values[2].put("id", "taroko");
	   values[2].put("name", "太鲁阁国家公园管理处");
	   values[2].put("phonoNo", "03-8621100");
	   values[2].put("address", "花莲县秀林乡258号");
	   
	   for(ContentValues row:values){
		   db.insert(table_name, null, row);
	   }
	   db.close();
	   
   }
   
   public ArrayList<String> getAddress(String name)
   {
	   SQLiteDatabase db=getReadableDatabase();
	   String sql="select name,address from "+table_name+" where name LIKE ?";
	   String args[]={"%"+name+"%"};
	   Cursor  cursor=db.rawQuery(sql, args);
	   ArrayList<String> addresses=new ArrayList<String>();
	   int columnCount=cursor.getColumnCount();
	   while(cursor.moveToNext())
	   {
		   String name_addr="";
		   for(int i=0;i<columnCount;i++)
			   name_addr+=cursor.getString(i)+"\n";
		   addresses.add(name_addr);
	   }
	   cursor.close();
	   db.close();
	   return addresses;
	   
	   
   }

}
